package dao;

import java.util.List;

import hibernate.HibernateUtil;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import dto.TipoTransporteDTO;
import entities.CargaEntity;
import entities.TamanioEntity;
import entities.TipoTransporteEntity;

public class TipoTransporteDAO {

	private static TipoTransporteDAO instancia = null;
	private static SessionFactory sf = null;
	
	private TipoTransporteDAO(){}
	
	public static TipoTransporteDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new TipoTransporteDAO();
		} 
		return instancia;
	}
	
	//TODO: HACER LOS METODOS QUE REALIZAN LAS CONSULTAS Y/O INSERTS
	/*public float totalPorCliente(dto.Cliente cliente)
	{
		SessionFactory sf =  HibernateUtil.getSessionFactory();
		Session session = sf.getCurrentSession();
		org.hibernate.Transaction t =  session.beginTransaction();
		Double resultado = (Double)session.createQuery("select sum(i.cantidad *  i.precio) as importe from Pedido p inner join p.items i where p.cliente.codigo = :codigoCliente").setInteger("codigoCliente", cliente.getCodigo()).uniqueResult();
		Float r = Float.valueOf(String.valueOf(resultado));
		t.commit();
		session = null;
		return r;
	}*/
	
	public void grabarTipoTransporte(TipoTransporteEntity tipoTransporte){
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(tipoTransporte);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public void actualizarTipoTransporte(TipoTransporteEntity tipoTransporte){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(tipoTransporte);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	
	public TipoTransporteEntity LoadTipoTransporte(int id) 
	{
		Session session = sf.openSession();
		session.beginTransaction();
		TipoTransporteEntity car = (TipoTransporteEntity) session.get(TipoTransporteEntity.class, id);
		session.flush();
		session.getTransaction().commit();
		session.close();
		return car;
	}

	public List<TipoTransporteEntity> obtenerTipos() {
		Session session = sf.openSession();
		session.beginTransaction();
		List<TipoTransporteEntity> tam = session.createQuery("from TipoTransporteEntity").list();
		session.flush();
		session.getTransaction().commit();
		session.close();
		return tam;	
	}
}
